<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="type1"
          @click="show"
          v-hasPermi="['output:cfmCheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="type1"
          @click="check_back = true"
          v-hasPermi="['output:cfmCheck:back']"
        >
          撤回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleDc('#cfmTable', '五金仓领料单据')"
          v-hasPermi="['output:cfmCheck:export']"
        >
          导出头表
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleDc('#cfmTableChild', '五金仓领料明细')"
          v-hasPermi="['output:cfmCheck:export']"
        >
          导出明细
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      @row-click="clickRow"
      :cell-style="iCellStyle"
      v-loading="loading"
      :data="cfmList"
      id="cfmTable"
      :height="tableH"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="29" align="center" />
      <el-table-column
        label="订单编号"
        :show-overflow-tooltip="true"
        width="110"
        align="center"
        prop="cfmDjbh"
      />
      <el-table-column
        label="领料日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cfmLlrq"
        width="121"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.cfmLlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="领料仓库"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.cfmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="部门代码"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfmBmdm"
      />
      <el-table-column
        label="描述"
        :show-overflow-tooltip="true"
        width="108"
        align="center"
        prop="cfmDesc"
      />
      <el-table-column
        label="拒收描述"
        :show-overflow-tooltip="true"
        width="108"
        align="center"
        prop="cfmNgdesc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cfmJlrq"
        width="73"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cfmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cfmShrq"
        width="73"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cfmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cfmGzrq"
        width="73"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.cfmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!--  明细-->
    <el-table
      highlight-current-row
      border
      stripe
      :data="cfdList"
      :height="tableHChild"
      id="cfmTableChild"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="29" align="center" />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="116"
        align="center"
        prop="cfdWldm"
      />
      <el-table-column
        label="规格描述"
        :show-overflow-tooltip="true"
        width="400"
        prop="itmPmgg"
      />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="53"
        align="center"
        prop="itmDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="存放位置"
        :show-overflow-tooltip="true"
        width="86"
        align="center"
        prop="cfdCwdm"
      >
        <template #default="scope">
          <dict-tag :options="allCwOptions" :value="scope.row.cfdCwdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="最小包装"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfdMinbz"
      />
      <el-table-column
        label="包装数量"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfdBzsl"
      />
      <el-table-column
        label="申请数量"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfdSqsl"
      />
      <el-table-column
        label="出库数量"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfdCksl"
      />
      <el-table-column
        label="出库人员"
        :show-overflow-tooltip="true"
        width="73"
        align="center"
        prop="cfdCkry"
      />
      <el-table-column
        label="出库日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="cfdCkrq"
        width="73"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.cfdCkrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-col :span="24">
          <el-form-item label="订单编号" prop="cfmDjbh">
            <el-input
              v-model="queryParams.cfmDjbh"
              placeholder="请输入订单编号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="领料日期">
            <el-date-picker
              v-model="daterangeCfmLlrq"
              size="small"
              style="width: 240px"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态标志">
            <el-radio-group v-model="queryParams.cfmZtbz">
              <el-radio
                v-for="dict in cfmZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 撤回对话框 -->
    <el-dialog
      :title="title"
      v-model="check_back"
      :close-on-click-modal="false"
      draggable
      width="600px"
      append-to-body
    >
      <el-form
        ref="rollbackForm"
        :model="rollbackForm"
        :rules="rollbackFormRules"
        label-width="100px"
      >
        <el-form-item label="撤回理由" prop="cfmNgdesc">
          <el-input
            v-model="rollbackForm.cfmNgdesc"
            placeholder="请输入撤回理由"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleBack">确 定</el-button>
        <el-button @click="check_back = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCfm,
  getCfdChild,
  getCfdByOrder,
  check,
  back,
} from "@/api/output/cfm";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "CfmCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      openCfd: false,
      check_back: false,
      cfmZtbzOptions: [],
      allStorageOptions: [],
      storageOptions: [],
      unitFlagOptions: [],
      allCwOptions: [],
      tableH: 250,
      tableHChild: 300,
      tableHAdd: 250,
      tableHAddChild: 300,
      type1: true,
      rollbackForm: {},
      rollbackFormRules: {
        cfmNgdesc: [
          { required: true, message: "请输入撤回理由", trigger: "blur" },
        ],
      },
      // 总条数
      total: 0,
      totalChild: 0,
      // 五金仓领料开单作业表格数据
      cfmList: [],
      cfdList: [],
      //五金仓领料添加数据
      cfmChildList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 领料日期时间范围
      daterangeCfmLlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cfmDjbh: null,
        cfmLlrq: null,
        cfmZtbz: "1",
        cfmTjflag: "1",
      },
      queryParamsForChild: {
        pageNum: 1,
        pageSize: 30,
        cfmDjbh: "",
        code: "",
        itmWldm: "",
        itmPmgg: "",
      },
      nowData: {
        cfmDjbh: "",
      },
      selectData: [],
      // 表单参数
      form: {},
      cfdForm: {
        itmWldm: "",
        itmPmgg: "",
      },
      // 表单校验
      rules: {},
    };
  },
  created() {
    //单位
    this.getAllDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    this.getDicts("sys_cjm_ztbz").then((response) => {
      this.cfmZtbzOptions = response.data;
    });
    //所有仓库
    getAll().then((response) => {
      this.allCwOptions = JSON.parse(JSON.stringify(response.data));

      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
      for (let item of this.allCwOptions) {
        item.dictLabel = item.storageDefaultstorage;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //有效仓库
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    this.getList();
  },
  methods: {
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.cfmZtbz == "1" && row.cfmTjflag == "1") {
        return "color:#0fac09";
      }
      //通过
      else if (row.cfmZtbz == "2" && row.cfmTjflag == "1") {
        return "color:#0323af";
      }
      //检验
      else if (row.cfmZtbz == "6" && row.cfmTjflag == "1") {
        return "color:#bb0331";
      }
    },

    /** 查询五金仓领料开单作业列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCfmLlrq && "" != this.daterangeCfmLlrq) {
        this.queryParams.params["beginCfmLlrq"] = this.moment(
          this.daterangeCfmLlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endCfmLlrq"] = this.moment(
          this.daterangeCfmLlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listCfm(this.queryParams).then((response) => {
        this.cfmList = [];
        this.cfdList = [];
        this.cfmList = this.cfmList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRow(row);
        } else if (this.cfmList.length > 0) {
          this.clickRow(this.cfmList[0]);
        }
      });
    },

    clickRow(row) {
      if (row) {
        this.nowData = row;
        this.ids = row.cfmId;
        this.type1 = !(
          row != null &&
          row.cfmZtbz == "1" &&
          row.cfmTjflag == "1"
        );
        this.getChild();
      }
    },

    getChild(row) {
      let order;
      if (row) {
        order = row.cfmDjbh;
      } else {
        order = this.nowData.cfmDjbh;
      }
      getCfdByOrder(order).then((res) => {
        this.cfdList = res.data;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listCfm(this.queryParams).then((response) => {
          this.cfmList = this.cfmList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cfmId: null,
        cfmDjbh: null,
        cfmLlrq: new Date(),
        cfmCkdm: null,
        cfmBmdm: null,
        cfmDesc: null,
        cfmTjflag: null,
        cfmNgdesc: null,
        cfmZtbz: null,
        cfmJlry: null,
        cfmJlrq: null,
        cfmShry: null,
        cfmShrq: null,
        cfmGzry: null,
        cfmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.cfmList = [];
      this.getList();
      this.openSearch = false;
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCfmLlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //赋值初始化
    handleGetCld(row) {
      this.queryParamsForChild.cfmDjbh = row.cfmDjbh;
      this.queryParamsForChild.code = row.cfmCkdm;
      this.handleProcessCld();
    },

    //开始传递参数
    handleProcessCld() {
      this.queryParamsForChild.pageNum = 1;
      getCfdChild(this.queryParamsForChild).then((res) => {
        this.cfmChildList = [];
        this.cfmChildList = this.cfmChildList.concat(res.rows);
        this.totalChild = res.total;
        this.openCfd = true;
        this.getChild();
      });
    },

    show() {
      const order = this.nowData.cfmDjbh;
      this.$confirm('是否确认审核领料编号为"' + order + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return check(order);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("审核成功");
        })
        .catch(() => {});
    },

    handleSearch() {
      this.reset();
      this.title = "查询五金仓领料开单作业";
      this.openSearch = true;
    },

    handleBack() {
      this.$refs["rollbackForm"].validate((valid) => {
        if (valid) {
          back(this.nowData.cfmDjbh, this.rollbackForm.cfmNgdesc).then(
            (response) => {
              this.check_back = false;
              this.msgSuccess("撤回成功");
              this.getList();
            }
          );
        }
      });
    },

    /** 导出按钮操作 */
    handleDc(a, b) {
      exportTable(a, b);
    },
  },
};
</script>

<style scoped>
>>> .el-dialog__body {
  padding: 10px 20px;
}
</style>
